#include <fstream>
#include <iostream>
#include <iomanip>
#include "TGraph.h"
#include "TLegend.h"
#include "TLegendEntry.h"
#include "TCanvas.h"
#include "TH1.h"
#include "TString.h"

using namespace std;

void EffPlot(){

  float REF[3]={0.95,0.9,0.7};
  int RR[2]={20,25};

  TCanvas *c1=new TCanvas("c1","c1",800,600);
  TH1 *frame=gPad->DrawFrame(5,1e-46,1000,1e-39);
  c1->SetLogx();
  c1->SetLogy();
  c1->SetGrid();
  frame->SetTitle("100kg #times 100days");
  frame->SetXTitle("WIMP Mass (GeV)");
  frame->SetYTitle("Cross Section (cm^{2})");

  TLegend *lg=new TLegend(0.6,0.65,0.83,0.9);
  lg->SetFillStyle(0);

  TGraph *gr1=new TGraph("./txt/90Limit2_0.0_1000.0.txt","%lf %lf");
  gr1->SetLineWidth(2);
  gr1->Draw("l"); 
  lg->AddEntry(gr1,"Ideal ( E_{R} > 0 )","l");

  TGraph *GR2[3];
  int col[3]={4,2,6};
  for(int i=0;i<2;i++){
    TGraph *GR1;
    GR1=new TGraph(Form("./txt/R%d_config%d_Alpha0.18_ref%.2f.txt",20,i+1,0.90),"%lf %lf");
    //GR1=new TGraph(Form("./txt/R%d_config%d_Alpha0.18_ref%.2f.txt",RR[i],1,0.90),"%lf %lf");
    //  else     GR1=new TGraph("./txt/config2_10%.txt","%lf %lf");
    GR2[i]=new TGraph();
    
    for(int j=0;j<gr1->GetN();j++){
      Double_t xxp,yyp;
      gr1->GetPoint(j,xxp,yyp);
      
      float ytmp;
      for(int k=0;k<GR1->GetN();k++){
	Double_t xxd,yyd;
	GR1->GetPoint(k,xxd,yyd);
	if( xxd==xxp ){
	  ytmp = yyd;
	  break;
	}
      }//k
      if(xxp==5.) cout<<ytmp<<endl;
      GR2[i]->SetPoint(j,xxp,yyp/ytmp);
    }//j
    
    GR2[i]->SetLineColor(2*(i+1));
    //    GR2[i]->SetLineColor(col[i]);
    GR2[i]->Draw("l");
    //lg->AddEntry(GR2[i],Form("#color[%d]{Ref : %d %}",col[i],(int)(100*REF[i]+0.5)),"l");
    //lg->AddEntry(GR2[i],Form("#color[%d]{#phi : %d cm}",2*(i+1),2*RR[i]),"l");
    if(i==0)lg->AddEntry(GR2[i],"#color[2]{Baseline}","l");
    if(i==1)lg->AddEntry(GR2[i],"#color[4]{Lower}","l");
    GR1->Delete();
  }
  
  //******************************
    ifstream fin1("./txt/WARP.txt");
    if(!fin1) return;

    TGraph *graph1=new TGraph();
    TGraph *graph2[2];
    double Mwimp,Limit;
    int ii=0;
    while( !fin1.eof() ){
      fin1 >> Mwimp >> Limit;
      graph1->SetPoint(ii, Mwimp, Limit);
      ii++;
    }
    graph1->SetLineColor(8);
    graph1->SetLineWidth(3);
    graph1->SetLineStyle(7);
    graph1->Draw("C");
    lg->AddEntry(graph1,"#color[8]{WARP 96.2 kg#timesdays}","l");

    for(int j=0;j<2;j++){
      ifstream fin2;
      if(j==0)fin2.open("./txt/DAMA_Na.txt");
      if(j==1)fin2.open("./txt/DAMA_I.txt");

      graph2[j]=new TGraph();
      ii=0;
      double tmpx,tmpy;
      while( fin2 >> tmpx >> tmpy ){
	graph2[j]->SetPoint(ii, tmpx, tmpy*1e-36);
	ii++;
      }
      graph2[j]->SetLineColor(9);
      graph2[j]->SetLineWidth(3);
      graph2[j]->SetLineStyle(7);
      graph2[j]->Draw("C");
    }
    
    lg->AddEntry(graph2[0],"#color[9]{DAMA}","l");
    lg->Draw();

    //-------------------------
}



void Eff(){

  TCanvas *c1=new TCanvas("c1","c1",800,600);
  TH1 *frame=gPad->DrawFrame(1,1e-5,1000,1);
  //  c1->SetLogx();
  //  c1->SetLogy();
  c1->SetGrid();
  frame->SetTitle("100kg #times 100days");
  frame->SetXTitle("WIMP Mass (GeV)");
  frame->SetYTitle("Cross Section (cm^{2})");

  TLegend *lg=new TLegend(0.6,0.65,0.83,0.9);
  lg->SetFillStyle(0);

  TGraph *GR1[3];

  for(int i=0;i<2;i++){
    
    GR1[i]=new TGraph(Form("./txt/config%d_alpha0.2.txt",i+1),"%lf %lf");

    GR1[i]->SetLineColor(2*(i+1));
    GR1[i]->Draw("p");
    lg->AddEntry(GR1[i],Form("#color[%d]{Config : %d}",2*(i+1),i+1),"l");
    //    GR1->Delete();
  }



}
